<template>
    <div class="app-container">
        <el-card shadow="never">
            <!-- 条件查询 -->
            <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="120px" @keyup.enter.native="submitSearch" @submit.native.prevent>
                <el-row>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="房间名称:" prop="roomName.value">
                            <el-input v-model="queryParams.roomName.value" placeholder="请输入房间名称" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="房间类型:" prop="roomType.value">
                            <el-select v-model="queryParams.roomType.value" placeholder="请选择房间类型"
                                       clearable>
                                <el-option v-for="(item, index) in roomType" :key="index" :label="item.label" :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="选择分类:" prop="category1.value">
                            <SelectCategory url="/admin/sq.category/tree" ref="category" :queryParams="{type: 1 }"
                                            :check-strictly="true"
                                            :value="[queryParams.category1.value, queryParams.category2.value, queryParams.category3.value]"
                                            @change="onChangeCategory"></SelectCategory>
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="创建时间:" prop="createTime.value">
                            <el-date-picker
                                v-model="queryParams.createTime.value"
                                type="datetimerange"
                                range-separator="至"
                                start-placeholder="开始"
                                end-placeholder="结束"
                                align="right"
                                value-format="yyyy-MM-dd HH:mm:ss"
                            >
                            </el-date-picker>
                          </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="关播时间:" prop="endTime.value">
                            <el-date-picker
                                v-model="queryParams.endTime.value"
                                type="datetimerange"
                                range-separator="至"
                                start-placeholder="开始"
                                end-placeholder="结束"
                                align="right"
                                value-format="yyyy-MM-dd HH:mm:ss"
                            >
                            </el-date-picker>
                          </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="主播:" prop="roomUserId.value">
                            <SelectPage v-model='queryParams.roomUserId.value' :url="'/admin/user/selectPage'"
                                        placeholder="请选择主播"
                                        show_field='nickname' show_id='uid' query_field='uid' :multiple='false' />
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="房间状态:" prop="roomStatus.value">
                            <el-select v-model="queryParams.roomStatus.value" placeholder="请选择房间状态" clearable>
                                <el-option v-for="(item, index) in roomStatus" :key="index" :label="item.label" :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item class="ele-text-right" label-width="50px">
                            <el-button type="primary" size="mini" @click="submitSearch" icon="el-icon-search" class="ele-btn-icon">搜索 </el-button>
                            <el-button @click="resetQuery" size="mini" icon="el-icon-refresh">重置</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="open" v-hasPermi="api.add">添加</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="api.delete">删除</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" v-loading="exportLoading" @click="handleExport" v-hasPermi="api.export">导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="initIndex" :columns="columns" printId="#quickadmin"></right-toolbar>
            </el-row>
            <!-- 列表 -->
            <qa-table
                :source="tableData"
                :columns="columns"
                :selection="true"
                :loading="loading"
                @sortChange="sortChange"
                @statusChange="statusChange"
                @selectionChange="selectionChange"
                id="quickadmin"
            >
                <template v-slot:action>
                    <el-table-column label="操作" width="180">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" icon="el-icon-edit" v-if="[1, 4, 5].includes(scope.row.roomStatus)"
                                       @click="handleEdit(scope.row)" v-hasPermi="api.edit">编辑</el-button>
                            <el-button size="mini" type="text" icon="el-icon-setting" @click="handleSetting(scope.row)" v-hasPermi="api.setting">设置</el-button>
                            <el-button size="mini" type="text" icon="el-icon-video-camera" @click="handleTranscribe(scope.row)" v-hasPermi="api.transcribe">回放</el-button>
                            <el-button size="mini" type="text" icon="el-icon-tickets" @click="handleDetail(scope.row)" v-hasPermi="api.find">详情</el-button>
                            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="api.delete">删除</el-button>
                            <el-button size="mini" type="text" icon="el-icon-document-checked" @click="handleGiftLog(scope.row)" v-hasPermi="api.giftLog">送礼记录</el-button>
                        </template>
                    </el-table-column>
                </template>
            </qa-table>
            <!-- 分页 -->
            <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
        </el-card>
        <!-- 编辑弹窗 --> 
       <el-dialog :title="!disabled ? ( findFormData[pk] ? '编辑' : '添加' ) : '详情'" :visible="editOpen" :before-close="beforeClose" destroy-on-close>
            <EditForm ref="editForm" :findFormData="findFormData" :disabled="disabled" v-if="editOpen"
                      @submitForm="submitForm" :editFormParams="editFormParams" @editFormCancel="beforeClose" />
        </el-dialog>
        <el-dialog title="点赞列表" :visible.sync="zanObj.visible"
                   class="dialog-no-pd"
                   width="60%" destroy-on-close append-to-body>
            <div style="height: 70vh;overflow-y: auto" v-if="zanObj.visible">
                <zanList :queryObj="zanObj"></zanList>
            </div>
        </el-dialog>
        <el-dialog title="用户列表" :visible.sync="userObj.visible"
                   class="dialog-no-pd"
                   width="60%" destroy-on-close append-to-body>
            <div style="height: 70vh;overflow-y: auto" v-if="userObj.visible">
                <userList :queryObj="userObj"></userList>
            </div>
        </el-dialog>
        <el-dialog title="消息列表" :visible.sync="msgObj.visible"
                   class="dialog-no-pd"
                   width="60%" destroy-on-close append-to-body>
            <div style="height: 70vh;overflow-y: auto" v-if="msgObj.visible">
                <messageList :queryObj="msgObj"></messageList>
            </div>
        </el-dialog>
        <el-dialog title="直播回放记录" :visible.sync="transcribeObj.visible"
                   class="dialog-no-pd"
                   width="60%" destroy-on-close append-to-body>
            <div style="height: 70vh;overflow-y: auto" v-if="transcribeObj.visible">
                <transcribeList :queryObj="transcribeObj"></transcribeList>
            </div>
        </el-dialog>
        <el-dialog title="直播间送礼记录" :visible.sync="giftLogObj.visible"
                   class="dialog-no-pd"
                   width="60%" destroy-on-close append-to-body>
            <div style="height: 70vh;overflow-y: auto" v-if="giftLogObj.visible">
                <giftLogList :queryObj="giftLogObj"></giftLogList>
            </div>
        </el-dialog>
        <el-dialog title='直播设置' :visible.sync='rateVisible'
                   class='dialog-no-pd'
                   width='50%' destroy-on-close append-to-body>
            <EditFormSetting :findFormData='settingFormData'
                             @editFormCancel='rateVisible=false' @submitForm='submitSetting'></EditFormSetting>
        </el-dialog>
    </div>
</template>
<script>
import { curdMixin } from '@/mixins/curdMixin'
import EditForm from './EditFormCom'
import SelectCategory from "@/components/My/SelectCategory";
import EditFormSetting from './EditFormSetting.vue'
import zanList from "../room_zan/index"
import userList from "../room_users/index"
import messageList from "../room_message"
import giftLogList from '../gift_log/index'
import transcribeList from '../room_transcribe/index'

export default {
    name: 'Curd',
    mixins: [curdMixin],
    components: { EditForm, EditFormSetting, SelectCategory, zanList, userList, messageList, giftLogList, transcribeList },
    data() {
        return {
            // table结构
            pk: 'id',
            columns: [
                {
                    visible: true,
                    label: '编号',
                    prop: 'id',
                    width: 55
                },
                {
                    visible: true,
                    label: '主播',
                    prop: 'roomUserName',
                },
                {
                    visible: true,
                    label: '房间名称',
                    prop: 'roomName',
                },
                {
                    visible: true,
                    label: '房间类型',
                    prop: 'roomType',
                    component:'QuickAdminTextColor',
                    formatter: (prop, row) => {
                        let type = ''
                        switch (prop) {
                            case 1:
                                type = 'success'
                                break
                            case 2:
                                type = 'warning'
                                break
                            case 3:
                                type = 'info'
                                break

                        }
                        return {
                            value: prop,
                            type: type,
                            text: row.roomType_text,
                        }
                    },
                },
                {
                    visible: true,
                    label: '直播分类',
                    prop: 'category1_text',
                    formatter: (prop, row)=> {
                        let arr = [
                            row.category1_text || '',
                            row.category2_text || '',
                            row.category3_text || '',
                        ].filter(el=>{
                            return !!el
                        })
                        return arr.join("、")
                    }
                },
                {
                    visible: false,
                    label: '付费价格',
                    prop: 'roomMoney',
                },
                // {
                //     visible: false,
                //     label: '房间密码',
                //     prop: 'roomPassword',
                // },
                {
                    visible: true,
                    label: '状态',
                    prop: 'roomStatus',
                    component:'QuickAdminTextColor',
                    width: 70,
                    formatter: (prop, row) => {
                        let type = ''
                        switch (prop) {
                            case 1:
                                type = 'success'
                                break
                            case 2:
                                type = 'warning'
                                break
                            case 3:
                                type = 'info'
                                break
                            case 5:
                                type = 'warning'
                                break
                        }
                        return {
                            value: prop,
                            type: type,
                            text: row.roomStatus_text,
                        }
                    },
                },
                {
                    visible: false,
                    label: '热门',
                    prop: 'is_hot',
                    component: "QuickAdminSwitch",
                    sortable: true
                },
                {
                    visible: true,
                    label: '关播时间',
                    prop: 'endTime',
                    width: 120,
                    overflow: true,
                    sortable: true
                },
                {
                    visible: true,
                    label: '用户数',
                    prop: 'userCount',
                    width: 90,
                    align: 'center',
                    sortable: true,
                    formatter: (prop, row)=> {
                        return `<a href="javascript:;" class="cBlue">${row.userCount}</a>`
                    },
                    click: (prop, row)=> { 
                        this.userObj.visible = true
                        this.userObj.roomId = row.id;
                        this.userObj.status = '';
                    }
                },
                {
                    visible: true,
                    label: '在线数',
                    prop: 'onlineCount',
                    width: 90,
                    align: 'center',
                    sortable: true,
                    formatter: (prop, row)=> {
                        return `<a href="javascript:;" class="cBlue">${row.onlineCount}</a>`
                    },
                    click: (prop, row)=> {
                        this.userObj.visible = true
                        this.userObj.roomId = row.id;
                        this.userObj.status = 1;
                    }
                },
                {
                    visible: true,
                    label: '点赞数',
                    prop: 'zanCount',
                    width: 90,
                    align: 'center',
                    sortable: true,
                    formatter: (prop, row)=> {
                        return `<a href="javascript:;" class="cBlue">${row.zanCount}</a>`
                    },
                    click: (prop, row)=> {
                        this.zanObj.visible = true
                        this.zanObj.roomId = row.id;
                    }
                },
                {
                    visible: true,
                    label: '消息数',
                    prop: 'msgCount',
                    width: 90,
                    align: 'center',
                    sortable: true,
                    formatter: (prop, row)=> {
                        return `<a href="javascript:;" class="cBlue">${row.msgCount}</a>`
                    },
                    click: (prop, row)=> {
                        this.msgObj.visible = true
                        this.msgObj.roomId = row.id;
                    }
                },
                {
                    visible: true,
                    label: '创建时间',
                    prop: 'createTime',
                    width: 120,
                    sortable: true,
                    overflow: true
                },
            ],
            // 接口地址
            api: {
                index: `/admin/live.room/index`,
                find: `/admin/live.room/find`,
                add: `/admin/live.room/add`,
                edit: `/admin/live.room/edit`,
                delete: `/admin/live.room/delete`,
                export: `/admin/live.room/export`,
                status: `/admin/live.room/status`,
                giftLog: `/admin/live.gift_log/index`,
                setting: `/admin/live.room/setting`,
                transcribe: `/admin/live.room_transcribe/index`,
            },
            editFormParams: {},
            // 搜索表单是否展开
            searchExpand: false,
            // 查询参数
            queryParams: {
                roomName: { value:undefined, op: '%like%'},
                roomType: { value:undefined, op: '='},
                roomStatus: { value:undefined, op: '='},
                category1: { value:undefined, op: '='},
                category2: { value:undefined, op: '='},
                category3: { value:undefined, op: '='},
                createTime: { value:[], op: 'range'},
                endTime: { value:[], op: 'range'},
                roomUserId: { value:[], op: '='},
            },
            roomType:[
               { label: "免费", value: 1 },
               { label: "付费", value: 2 },
               // { label: "加密", value: 3 },
            ],
            roomStatus:[
                { label: "待审核", value: 1 },
                { label: "开播中", value: 2 },
                { label: "已下播", value: 3 },
                { label: "待开播", value: 4 },
                { label: "审核失败", value: 5 },
            ],
            settingFormData: {},
            rateVisible: false,
            zanObj: {
                visible: false,
                roomId: ''
            },
            userObj: {
                visible: false,
                roomId: '',
                status: ''
            },
            msgObj: {
                visible: false,
                roomId: ''
            },
            giftLogObj: {
                visible: false,
                roomId: ''
            },
            transcribeObj: {
                visible: false,
                room_id: ''
            }
        }
    },
    methods: {
        submitSetting(e) {
            this.settingFormData = e
            this.request
                .post(this.api.setting, e)
                .then(response => {
                    this.msgSuccess('修改成功')
                    this.settingFormData = {}
                    this.initIndex()
                    this.rateVisible = false
                })
        },
        onChangeCategory(e, pathLabels) {
            e = e || []
            pathLabels = pathLabels || []
            this.$set(this.queryParams.category3, 'value', e.length >= 3 ? e[2] : '')
            this.$set(this.queryParams.category2, 'value', e.length >= 2 ? e[1] : '')
            this.$set(this.queryParams.category1, 'value', e.length >= 1 ? e[0] : '')
        },
        handleSetting(row) {
            this.rateVisible = true;
            this.settingFormData = row
        },
        handleGiftLog(row) {
            this.giftLogObj = {
                visible: true,
                roomId: row.id
            }
        },
        handleTranscribe(row) {
            this.transcribeObj = {
                visible: true,
                room_id: row.id
            }
        }
    }
}
</script>
